home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / test / test_array.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  2.8 KB  |  137 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Test the arraymodule.
  5.    Roger E. Masse
  6. '''
  7. import array
  8. TestFailed
  9.  
  10. def main():
  11.     testtype('c', 'c')
  12.     for type in [
  13.         'b',
  14.         'h',
  15.         'i',
  16.         'l',
  17.         'f',
  18.         'd']:
  19.         testtype(type, 1)
  20.     
  21.     unlink(TESTFN)
  22.  
  23.  
  24. def testtype(type, example):
  25.     a = array.array(type)
  26.     a.append(example)
  27.     if verbose:
  28.         print 40 * '*'
  29.         print 'array after append: ', a
  30.     
  31.     a.typecode
  32.     a.itemsize
  33.     if a.typecode in ('i', 'b', 'h', 'l'):
  34.         a.byteswap()
  35.     
  36.     if a.typecode == 'c':
  37.         f = open(TESTFN, 'w')
  38.         f.write('The quick brown fox jumps over the lazy dog.\n')
  39.         f.close()
  40.         f = open(TESTFN, 'r')
  41.         a.fromfile(f, 10)
  42.         f.close()
  43.         if verbose:
  44.             print 'char array with 10 bytes of TESTFN appended: ', a
  45.         
  46.         a.fromlist([
  47.             'a',
  48.             'b',
  49.             'c'])
  50.         if verbose:
  51.             print 'char array with list appended: ', a
  52.         
  53.     
  54.     a.insert(0, example)
  55.     if verbose:
  56.         print 'array of %s after inserting another:' % a.typecode, a
  57.     
  58.     f = open(TESTFN, 'w')
  59.     a.tofile(f)
  60.     f.close()
  61.     a.tolist()
  62.     a.tostring()
  63.     if verbose:
  64.         print 'array of %s converted to a list: ' % a.typecode, a.tolist()
  65.     
  66.     if verbose:
  67.         print 'array of %s converted to a string: ' % a.typecode, `a.tostring()`
  68.     
  69.     if type == 'c':
  70.         a = array.array(type, 'abcde')
  71.         a[:-1] = a
  72.         if a != array.array(type, 'abcdee'):
  73.             raise TestFailed, 'array(%s) self-slice-assign (head)' % `type`
  74.         
  75.         a = array.array(type, 'abcde')
  76.         a[1:] = a
  77.         if a != array.array(type, 'aabcde'):
  78.             raise TestFailed, 'array(%s) self-slice-assign (tail)' % `type`
  79.         
  80.         a = array.array(type, 'abcde')
  81.         a[1:-1] = a
  82.         if a != array.array(type, 'aabcdee'):
  83.             raise TestFailed, 'array(%s) self-slice-assign (cntr)' % `type`
  84.         
  85.     else:
  86.         a = array.array(type, [
  87.             1,
  88.             2,
  89.             3,
  90.             4,
  91.             5])
  92.         a[:-1] = a
  93.         if a != array.array(type, [
  94.             1,
  95.             2,
  96.             3,
  97.             4,
  98.             5,
  99.             5]):
  100.             raise TestFailed, 'array(%s) self-slice-assign (head)' % `type`
  101.         
  102.         a = array.array(type, [
  103.             1,
  104.             2,
  105.             3,
  106.             4,
  107.             5])
  108.         a[1:] = a
  109.         if a != array.array(type, [
  110.             1,
  111.             1,
  112.             2,
  113.             3,
  114.             4,
  115.             5]):
  116.             raise TestFailed, 'array(%s) self-slice-assign (tail)' % `type`
  117.         
  118.         a = array.array(type, [
  119.             1,
  120.             2,
  121.             3,
  122.             4,
  123.             5])
  124.         a[1:-1] = a
  125.         if a != array.array(type, [
  126.             1,
  127.             1,
  128.             2,
  129.             3,
  130.             4,
  131.             5,
  132.             5]):
  133.             raise TestFailed, 'array(%s) self-slice-assign (cntr)' % `type`
  134.         
  135.  
  136. main()
  137.